package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.OrderConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NumberUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.client.MesProductionQueue;
import com.ruoyi.system.domain.client.MesProductionQueueDTO;
import com.ruoyi.system.domain.function.ProdOrder;
import com.ruoyi.system.mapper.BaseMaterialMapper;
import com.ruoyi.system.mapper.MesProductionQueueMapper;
import com.ruoyi.system.mapper.ProdOrderMapper;
import com.ruoyi.system.service.IMesProductionQueueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * mes生产队列Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-20
 */
@Service
public class MesProductionQueueServiceImpl extends ServiceImpl<MesProductionQueueMapper, MesProductionQueue> implements IMesProductionQueueService {

    @Autowired
    private MesProductionQueueMapper mesProductionQueueMapper;

    @Autowired
    private ProdOrderMapper prodOrderMapper;

    @Override
    public int createProductionQueue(MesProductionQueueDTO dto) {
        int orderNumber = dto.getOrderNumber();
        int i = 0;
        for (int j = 1; j <= orderNumber; j++) {
            MesProductionQueue mesProductionQueue = new MesProductionQueue();
            String serialNumber = NumberUtil.getSerialNumber("mes");
            mesProductionQueue.setProductCode(dto.getProductCode());
            mesProductionQueue.setOrderCode(dto.getOrderCode());
            mesProductionQueue.setSequence(j);
            mesProductionQueue.setSerialNumber(serialNumber);
            mesProductionQueue.setCreateTime(DateUtils.getNowDate());
            mesProductionQueue.setCreateBy(SecurityUtils.getUsername());
            i = mesProductionQueueMapper.insert(mesProductionQueue);
        }
        ProdOrder po = prodOrderMapper.selectById(dto.getOrderId());
        if(po != null){
            po.setOrderState(OrderConstants.Implementation);
            prodOrderMapper.updateById(po);
        }
        return i;
    }
}
